<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/dute_favicon_32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/dute_favicon_16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="manifest" href="/images/manifest.json">
  <meta name="msapplication-config" content="/images/browserconfig.xml">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="google-site-verification" content="mpI5dkydstZXl6UcDCppqktXK0bbvqdZ6LkZ3KNk4Iw">
  <meta name="baidu-site-verification" content="code-a1LksZX2Ds">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"whitestore.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="很早之前做的笔记了，做一个备忘">
<meta property="og:type" content="article">
<meta property="og:title" content="数据结构和算法的一个留坑">
<meta property="og:url" content="https://whitestore.top/2020/10/27/study/index.html">
<meta property="og:site_name" content="爱看书的阿东">
<meta property="og:description" content="很早之前做的笔记了，做一个备忘">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-10-27T15:01:35.000Z">
<meta property="article:modified_time" content="2023-07-16T06:28:09.262Z">
<meta property="article:author" content="阿东">
<meta property="article:tag" content="数据结构">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://whitestore.top/2020/10/27/study/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>数据结构和算法的一个留坑 | 爱看书的阿东</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="爱看书的阿东" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">爱看书的阿东</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">赐他一块白色石头，石头上写着新名</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://whitestore.top/2020/10/27/study/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="阿东">
      <meta itemprop="description" content="随遇而安">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱看书的阿东">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          数据结构和算法的一个留坑
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-10-27 23:01:35" itemprop="dateCreated datePublished" datetime="2020-10-27T23:01:35+08:00">2020-10-27</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-07-16 14:28:09" itemprop="dateModified" datetime="2023-07-16T14:28:09+08:00">2023-07-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">数据结构与算法</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/10/27/study/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/10/27/study/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>16k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>14 分钟</span>
            </span>
            <div class="post-description">很早之前做的笔记了，做一个备忘</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="1-数据结构与算法入门"><a href="#1-数据结构与算法入门" class="headerlink" title="1. 数据结构与算法入门"></a>1. 数据结构与算法入门</h1><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>很早之前做的笔记了，做一个备忘</p>
<a id="more"></a>

<h2 id="1-1-经典算法问题："><a href="#1-1-经典算法问题：" class="headerlink" title="1.1 经典算法问题："></a>1.1 经典算法问题：</h2><ol>
<li>汉诺塔</li>
<li>八皇后问题</li>
<li>马踏棋盘</li>
</ol>
<h2 id="1-2-字符串匹配"><a href="#1-2-字符串匹配" class="headerlink" title="1.2 字符串匹配"></a>1.2 字符串匹配</h2><h3 id="1-2-1-暴力匹配"><a href="#1-2-1-暴力匹配" class="headerlink" title="1.2.1 暴力匹配"></a>1.2.1 暴力匹配</h3><h3 id="1-2-2-KMP算法"><a href="#1-2-2-KMP算法" class="headerlink" title="1.2.2 KMP算法"></a>1.2.2 KMP算法</h3><h2 id="1-3-数据结构和算法重要性"><a href="#1-3-数据结构和算法重要性" class="headerlink" title="1.3 数据结构和算法重要性"></a>1.3 数据结构和算法重要性</h2><ol>
<li>算法是程序灵魂</li>
<li>内存计算框架</li>
</ol>
<h2 id="1-4-数据结构与算法关系"><a href="#1-4-数据结构与算法关系" class="headerlink" title="1.4 数据结构与算法关系"></a>1.4 数据结构与算法关系</h2><h1 id="2-实际算法问题："><a href="#2-实际算法问题：" class="headerlink" title="2. 实际算法问题："></a>2. 实际算法问题：</h1><h2 id="2-1-str-replaceAll-str"><a href="#2-1-str-replaceAll-str" class="headerlink" title="2.1 str.replaceAll( str )"></a>2.1 str.replaceAll( str )</h2><h3 id="2-1-1-问题："><a href="#2-1-1-问题：" class="headerlink" title="2.1.1 问题："></a>2.1.1 问题：</h3><p>试写出单链表表示的字符串类以及字符串结点类的定义，并且依次实现它的构造函数，以及计算串的长度，串赋值，判断两串相等，求子串，两串连接，求子串在串中位置等七个成员函数</p>
<h2 id="2-2-其他几个问题："><a href="#2-2-其他几个问题：" class="headerlink" title="2.2 其他几个问题："></a>2.2 其他几个问题：</h2><ol>
<li>丢手帕问题</li>
<li>磁盘问题</li>
<li>公交车</li>
<li>画图</li>
<li>球和篮子</li>
<li>扔石头</li>
<li>修路问题，最小路径</li>
<li>最短路径问题</li>
<li>汉诺塔</li>
<li>八皇后</li>
</ol>
<h2 id="2-3-线性结构与非线性结构"><a href="#2-3-线性结构与非线性结构" class="headerlink" title="2.3 线性结构与非线性结构"></a>2.3 线性结构与非线性结构</h2><ol>
<li>数据与元素一对一的线性关系</li>
<li>顺序存储，元素都是连续的</li>
<li>链式存储，元素是不连续的</li>
<li>数组，队列，链表和栈</li>
</ol>
<h3 id="2-3-1-非线性结构"><a href="#2-3-1-非线性结构" class="headerlink" title="2.3.1 非线性结构"></a>2.3.1 非线性结构</h3><p>二维数组，多维数组，广义表，树结构，图结构</p>
<h1 id="3-稀疏数组和队列"><a href="#3-稀疏数组和队列" class="headerlink" title="3. 稀疏数组和队列"></a>3. 稀疏数组和队列</h1><h2 id="3-1-稀疏数组的处理方法："><a href="#3-1-稀疏数组的处理方法：" class="headerlink" title="3.1 稀疏数组的处理方法："></a>3.1 稀疏数组的处理方法：</h2><ol>
<li>记录数组一共几行几列，有多少个不同的值</li>
<li>把具有不同值的元素的行列记录在一个小规模数组</li>
</ol>
<h3 id="3-1-1-二维数组转稀疏数组的方法"><a href="#3-1-1-二维数组转稀疏数组的方法" class="headerlink" title="3.1.1 二维数组转稀疏数组的方法"></a>3.1.1 二维数组转稀疏数组的方法</h3><ol>
<li>遍历原始二维数组，保留有效个数</li>
<li>根据sum创建稀疏数组spareArr int[sum+1][3]</li>
<li>二维数组的有效数据存入到稀疏数组</li>
</ol>
<h2 id="3-2-稀疏数组转二维数组："><a href="#3-2-稀疏数组转二维数组：" class="headerlink" title="3.2 稀疏数组转二维数组："></a>3.2 稀疏数组转二维数组：</h2><ol>
<li>先读取稀疏数组的第一行，根据第一行的数据，创建原始的二维数组，</li>
<li>读取稀疏数组的后几行数据，并赋值给原始的二维数组</li>
</ol>
<h1 id="4-稀疏数组的Java代码实现"><a href="#4-稀疏数组的Java代码实现" class="headerlink" title="4. 稀疏数组的Java代码实现"></a>4. 稀疏数组的Java代码实现</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> array;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 稀疏数组</span></span><br><span class="line"><span class="comment"> * 1. 需要将二维数组转为稀疏数组存储</span></span><br><span class="line"><span class="comment"> * 2. 稀疏数组进行保存（文件读写）</span></span><br><span class="line"><span class="comment"> * 3. 读取文件恢复稀疏数组（文件读写）</span></span><br><span class="line"><span class="comment"> * 4. 将稀疏数组转回二维数组</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhaoxudong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span>: SpareceArray</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@projectName</span> structAlgorithms</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 稀疏数组</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/8/19 14:54</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpareceArray</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用稀疏数组</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> args 数组</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[][] twoArrayConvertSparecArray = twoArrayConvertSparecArray();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 稀疏数组如何转为二维住</span></span><br><span class="line">        SparecArrayConverttwoArray(twoArrayConvertSparecArray);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 稀疏数组转为二维数组</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">SparecArrayConverttwoArray</span><span class="params">(<span class="keyword">int</span>[][] array)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 1. 根据第一行数据还原出二维数组的行与列</span></span><br><span class="line">        <span class="keyword">int</span>[][] result = <span class="keyword">new</span> <span class="keyword">int</span>[array[<span class="number">0</span>][<span class="number">0</span>]][array[<span class="number">0</span>][<span class="number">1</span>]];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; array.length; i++) &#123;</span><br><span class="line">            result[array[i][<span class="number">0</span>]][array[i][<span class="number">1</span>]] = array[i][<span class="number">2</span>];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        printArray(array);</span><br><span class="line">        printArray(result);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 稀疏数组转为二维数组的办法</span></span><br><span class="line"><span class="comment">     * 1. 创建二维数组，并且加入数据</span></span><br><span class="line"><span class="comment">     * 2.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span>[][] twoArrayConvertSparecArray() &#123;</span><br><span class="line">        <span class="comment">// 将二维数组转为稀疏数组 初始化为 11 11</span></span><br><span class="line">        <span class="keyword">int</span>[][] array = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">8</span>][<span class="number">8</span>];</span><br><span class="line">        List list = <span class="keyword">new</span> ArrayList();</span><br><span class="line">        <span class="comment">// 在二维数组放两个子</span></span><br><span class="line">        array[<span class="number">4</span>][<span class="number">5</span>] = <span class="number">2</span>;</span><br><span class="line">        array[<span class="number">3</span>][<span class="number">7</span>] = <span class="number">11</span>;</span><br><span class="line">        array[<span class="number">2</span>][<span class="number">3</span>] = <span class="number">11</span>;</span><br><span class="line">        printArray(array);</span><br><span class="line">        <span class="comment">// 计算有几个有效数据</span></span><br><span class="line">        <span class="comment">// 存储有几个有效数据</span></span><br><span class="line">        <span class="keyword">int</span> sumCount = <span class="number">0</span>;</span><br><span class="line">        sumCount = calcuArrSize(array, sumCount);</span><br><span class="line">        <span class="comment">// 创建稀疏数组</span></span><br><span class="line">        <span class="keyword">int</span>[][] spareceArray = <span class="keyword">new</span> <span class="keyword">int</span>[sumCount+<span class="number">1</span>][<span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 第一行为 个数 棋盘的行 棋盘的列</span></span><br><span class="line">        spareceArray[<span class="number">0</span>][<span class="number">0</span>] = array.length;</span><br><span class="line">        spareceArray[<span class="number">0</span>][<span class="number">1</span>] = array[<span class="number">0</span>].length;</span><br><span class="line">        spareceArray[<span class="number">0</span>][<span class="number">2</span>] = sumCount;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 稀疏数组存储数据</span></span><br><span class="line">        <span class="comment">//count 用于记录是第几个非0数据</span></span><br><span class="line">        <span class="keyword">int</span> noZeroCount = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; array.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; array[i].length; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span>(array[i][j] != <span class="number">0</span>) &#123;</span><br><span class="line">                    noZeroCount++;</span><br><span class="line">                    <span class="comment">// 存储第几行 第几列 值为多少</span></span><br><span class="line">                    spareceArray[noZeroCount][<span class="number">0</span>] = i;</span><br><span class="line">                    spareceArray[noZeroCount][<span class="number">1</span>] = j;</span><br><span class="line">                    spareceArray[noZeroCount][<span class="number">2</span>] = array[i][j];</span><br><span class="line"></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> spareceArray;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 计算有效数据的个数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> array 原有的数组</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> sumCount 计算个数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">calcuArrSize</span><span class="params">(<span class="keyword">int</span>[][] array, <span class="keyword">int</span> sumCount)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; array.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; array[i].length; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span>(array[i][j] != <span class="number">0</span>) &#123;</span><br><span class="line">                    sumCount++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sumCount;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 打印数组的快捷方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> array 数组</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">printArray</span><span class="params">(<span class="keyword">int</span>[][] array)</span> </span>&#123;</span><br><span class="line">        System.err.println(<span class="string">"-----------------我是分割线-----------------"</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; array.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; array[i].length; j++) &#123;</span><br><span class="line">                System.out.print(array[i][j] + <span class="string">"\t"</span> );</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">        System.err.println(<span class="string">"-----------------我是分割线-----------------"</span>);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="5-队列："><a href="#5-队列：" class="headerlink" title="5. 队列："></a>5. 队列：</h1><h2 id="5-1-数组模拟队列"><a href="#5-1-数组模拟队列" class="headerlink" title="5.1 数组模拟队列"></a>5.1 数组模拟队列</h2><h3 id="5-1-1-思路分析：addQueue"><a href="#5-1-1-思路分析：addQueue" class="headerlink" title="5.1.1 思路分析：addQueue"></a>5.1.1 思路分析：addQueue</h3><ol>
<li>将尾指针 rear + 1 ， 表示入队。当 rear == front 表示空队列</li>
<li>如果rear 等于队列 maxSize - 1， 表示队列满，否则可以增加元素</li>
</ol>
<h3 id="5-1-2-思路分析：removeQueue"><a href="#5-1-2-思路分析：removeQueue" class="headerlink" title="5.1.2 思路分析：removeQueue"></a>5.1.2 思路分析：removeQueue</h3><ol>
<li>将队头指针进行出队操作</li>
<li>每次移除都需要队头指针 +1</li>
<li>将队列后面的数据向前拷贝</li>
</ol>
<h2 id="5-2-JAVA代码实现数组底层的队列"><a href="#5-2-JAVA代码实现数组底层的队列" class="headerlink" title="5.2 JAVA代码实现数组底层的队列"></a>5.2 JAVA代码实现数组底层的队列</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> array;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.sql.rowset.serial.SerialArray;</span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用数组实现队列</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhaoxudong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span>: MyQueue</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@projectName</span> structAlgorithms</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 使用数组实现队列</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/8/23 15:27</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyQueue</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 队尾指针</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> rear;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 队头指针</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> front;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 最大数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> maxSize;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 队列</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span>[] arr;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MyQueue</span><span class="params">(<span class="keyword">int</span> size)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (size &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">            System.err.println(<span class="string">"队列不能小于或者等于 0"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 注意此处为什么是-1 ：因为队列的头部不应该执行队伍的第一个元素，因为此时是出于队列的头部，并没有数据</span></span><br><span class="line">        rear = front = -<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">this</span>.arr = <span class="keyword">new</span> <span class="keyword">int</span>[size];</span><br><span class="line">        <span class="keyword">this</span>.maxSize = size;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 出队操作：</span></span><br><span class="line"><span class="comment">     * 1. 队伍头部的元素删除即，使 return arr[front]</span></span><br><span class="line"><span class="comment">     * 2. 将队伍后面的元素向前拷贝</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">removeQueue</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (isEmpty()) &#123;</span><br><span class="line">            System.err.println(<span class="string">"队列为空，不能移除"</span>);</span><br><span class="line">            <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; rear; i++) &#123;</span><br><span class="line">            arr[i - <span class="number">1</span>] = arr[i];</span><br><span class="line">        &#125;</span><br><span class="line"><span class="comment">//        arr[--rear] = 0;</span></span><br><span class="line">        <span class="keyword">return</span> arr[++front];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 队尾+1 入队操作</span></span><br><span class="line"><span class="comment">     * 需要判断队列是否为空并且是否不满</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> val</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addQueue</span><span class="params">(<span class="keyword">int</span> val)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 判断是否为空或者是否已经满了</span></span><br><span class="line">        <span class="keyword">boolean</span> empty = isFull();</span><br><span class="line">        <span class="keyword">if</span> (!empty) &#123;</span><br><span class="line">            System.err.println(<span class="string">"队列已满，无法添加"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        arr[++rear] = val;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断队列是否可以加入数据</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">isFull</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> rear != maxSize - <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> rear == front;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showQueue</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.err.println(Arrays.toString(<span class="keyword">this</span>.arr));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        MyQueue myQueue = <span class="keyword">new</span> MyQueue(<span class="number">3</span>);</span><br><span class="line">        myQueue.addQueue(<span class="number">2</span>);</span><br><span class="line">        myQueue.addQueue(<span class="number">5</span>);</span><br><span class="line">        myQueue.addQueue(<span class="number">6</span>);</span><br><span class="line">        myQueue.removeQueue();</span><br><span class="line">        myQueue.removeQueue();</span><br><span class="line">        myQueue.removeQueue();</span><br><span class="line">        <span class="comment">// 这里出现问题，因为队头和队尾都指向队列的尾部，会出现无法添加和无法删除掉问题</span></span><br><span class="line">        myQueue.addQueue(<span class="number">6</span>);</span><br><span class="line">        myQueue.removeQueue();</span><br><span class="line"></span><br><span class="line">        myQueue.showQueue();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="5-2-1-数组队列的弊端"><a href="#5-2-1-数组队列的弊端" class="headerlink" title="5.2.1 数组队列的弊端"></a>5.2.1 数组队列的弊端</h3><ul>
<li>队头和队尾都执行队列的尾部时候，无法添加也无法删除<ul>
<li>数组只能使用一次</li>
</ul>
</li>
<li>数组复用性不好</li>
</ul>
<h2 id="5-3-数组实现循环队列"><a href="#5-3-数组实现循环队列" class="headerlink" title="5.3  数组实现循环队列"></a>5.3  数组实现循环队列</h2><h3 id="5-3-1-如何判定队列已经满了"><a href="#5-3-1-如何判定队列已经满了" class="headerlink" title="5.3.1 如何判定队列已经满了"></a>5.3.1 如何判定队列已经满了</h3><p>算法</p>
<blockquote>
<p>( rear + 1 ) % maxSize == front</p>
</blockquote>
<h3 id="5-3-2-如何判定队列是空的"><a href="#5-3-2-如何判定队列是空的" class="headerlink" title="5.3.2 如何判定队列是空的"></a>5.3.2 如何判定队列是空的</h3><p><font color='red'><strong>rear == front</strong></font></p>
<h3 id="5-3-3-如何判定有效个数"><a href="#5-3-3-如何判定有效个数" class="headerlink" title="5.3.3 如何判定有效个数"></a>5.3.3 如何判定有效个数</h3><blockquote>
<p>举例：</p>
<p>maxSize = 7</p>
<p>front = 5</p>
<p>rear =  5 </p>
<p>maxSize + (rear+1) - front % maxSize</p>
</blockquote>
<h2 id="5-3-4-使用数组实现循环队列的方式2"><a href="#5-3-4-使用数组实现循环队列的方式2" class="headerlink" title="5.3.4 使用数组实现循环队列的方式2"></a>5.3.4 使用数组实现循环队列的方式2</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> array;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用数组实现队列</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhaoxudong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span>: MyQueue</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@projectName</span> structAlgorithms</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 使用数组实现队列</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/8/23 15:27</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyQueue</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 队尾指针</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> rear;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 队头指针</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> front;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 最大数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> maxSize;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 队列</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span>[] arr;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MyQueue</span><span class="params">(<span class="keyword">int</span> size)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (size &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">            System.err.println(<span class="string">"队列不能小于或者等于 0"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 注意此处为什么是-1 ：因为队列的头部不应该执行队伍的第一个元素，因为此时是出于队列的头部，并没有数据</span></span><br><span class="line">        rear = front = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">this</span>.arr = <span class="keyword">new</span> <span class="keyword">int</span>[size];</span><br><span class="line">        <span class="keyword">this</span>.maxSize = size;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 出队操作：</span></span><br><span class="line"><span class="comment">     * 1. 队伍头部的元素删除即，使 return arr[front]</span></span><br><span class="line"><span class="comment">     * 2. 将队伍后面的元素向前拷贝</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">removeQueue</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (isEmpty()) &#123;</span><br><span class="line">            System.err.println(<span class="string">"队列为空，不能移除"</span>);</span><br><span class="line">            <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="comment">//        for (int i = 1; i &lt; rear; i++) &#123;</span></span><br><span class="line"><span class="comment">//            arr[i - 1] = arr[i];</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//        arr[--rear] = 0;</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> arr[++front];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 队尾+1 入队操作</span></span><br><span class="line"><span class="comment">     * 需要判断队列是否为空并且是否不满</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> val</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addQueue</span><span class="params">(<span class="keyword">int</span> val)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 判断是否为空或者是否已经满了</span></span><br><span class="line">        <span class="keyword">boolean</span> empty = isFull();</span><br><span class="line">        <span class="keyword">if</span> (empty) &#123;</span><br><span class="line">            System.err.println(<span class="string">"队列已满，无法添加"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        arr[rear] = val;</span><br><span class="line">        rear = (rear + <span class="number">1</span>) % maxSize;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断队列是否可以加入数据</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">isFull</span><span class="params">()</span> </span>&#123;</span><br><span class="line"><span class="comment">//        return rear != maxSize - 1;</span></span><br><span class="line">        <span class="comment">// 这是最新的一种判定方式： rear =</span></span><br><span class="line">        <span class="keyword">return</span> (rear  + <span class="number">1</span>) % maxSize == front;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> rear == front;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showQueue</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(arr == <span class="keyword">null</span> || arr.length == <span class="number">0</span>)&#123;</span><br><span class="line">            System.err.println(<span class="string">"队列为空，不能遍历"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = front; i &lt; front + size(); i++) &#123;</span><br><span class="line">            System.err.print(arr[i % maxSize] + <span class="string">"\t"</span>);</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line"><span class="comment">//        System.err.println(Arrays.toString(this.arr));</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (rear + maxSize - front) % maxSize;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        MyQueue myQueue = <span class="keyword">new</span> MyQueue(<span class="number">8</span>);</span><br><span class="line">        myQueue.addQueue(<span class="number">2</span>);</span><br><span class="line">        myQueue.addQueue(<span class="number">55</span>);</span><br><span class="line">        myQueue.addQueue(<span class="number">35</span>);</span><br><span class="line">        myQueue.addQueue(<span class="number">25</span>);</span><br><span class="line">        myQueue.addQueue(<span class="number">65</span>);</span><br><span class="line">        myQueue.addQueue(<span class="number">85</span>);</span><br><span class="line">        myQueue.addQueue(<span class="number">75</span>);</span><br><span class="line"><span class="comment">//        myQueue.addQueue(6);</span></span><br><span class="line">        myQueue.removeQueue();</span><br><span class="line">        myQueue.removeQueue();</span><br><span class="line">        myQueue.removeQueue();</span><br><span class="line">        <span class="comment">// 这里出现问题，因为队头和队尾都指向队列的尾部，会出现无法添加和无法删除掉问题</span></span><br><span class="line"><span class="comment">//        myQueue.addQueue(6);</span></span><br><span class="line"><span class="comment">//        myQueue.removeQueue();</span></span><br><span class="line"></span><br><span class="line">        myQueue.showQueue();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="6-链表"><a href="#6-链表" class="headerlink" title="6. 链表"></a>6. 链表</h1><h2 id="6-1-使用自制的链表处理"><a href="#6-1-使用自制的链表处理" class="headerlink" title="6.1 使用自制的链表处理"></a>6.1 使用自制的链表处理</h2><h3 id="6-1-1-使用java代码实现链表"><a href="#6-1-1-使用java代码实现链表" class="headerlink" title="6.1.1 使用java代码实现链表"></a>6.1.1 使用java代码实现链表</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> queue;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 单链表实现</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhaoxudong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span>: SpareceArray</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@projectName</span> structAlgorithms</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 单链表实现</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/8/19 14:54</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SingelQueue</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        HeroNode heroNode1 = <span class="keyword">new</span> HeroNode(<span class="number">0</span>, <span class="string">"111"</span>, <span class="string">"111"</span>);</span><br><span class="line">        HeroNode heroNode2 = <span class="keyword">new</span> HeroNode(<span class="number">0</span>, <span class="string">"222"</span>, <span class="string">"222"</span>);</span><br><span class="line">        HeroNode heroNode3 = <span class="keyword">new</span> HeroNode(<span class="number">0</span>, <span class="string">"333"</span>, <span class="string">"333"</span>);</span><br><span class="line">        HeroNode heroNode4 = <span class="keyword">new</span> HeroNode(<span class="number">0</span>, <span class="string">"444"</span>, <span class="string">"444"</span>);</span><br><span class="line">        SingelLinkedList singelLinkedList = <span class="keyword">new</span> SingelLinkedList();</span><br><span class="line">        singelLinkedList.add(heroNode1);</span><br><span class="line">        singelLinkedList.add(heroNode2);</span><br><span class="line">        singelLinkedList.add(heroNode3);</span><br><span class="line">        singelLinkedList.add(heroNode4);</span><br><span class="line">        singelLinkedList.list();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SingelLinkedList</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> HeroNode head = <span class="keyword">new</span> HeroNode(<span class="number">0</span>, <span class="string">""</span>, <span class="string">""</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 在插入的使用最新的方式：</span></span><br><span class="line"><span class="comment">     * 根据id编号进行排序</span></span><br><span class="line"><span class="comment">     * 1.</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> node</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(HeroNode node)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 1. 由于头结点不能动，需要使用</span></span><br><span class="line">        HeroNode heroNode = head;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span>(heroNode.getNext() == <span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            heroNode = heroNode.getNext();</span><br><span class="line">        &#125;</span><br><span class="line">        heroNode.setNext(node);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">list</span><span class="params">()</span></span>&#123;</span><br><span class="line">        HeroNode heroNode = head;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span>(heroNode.getNext() == <span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            System.err.println(heroNode);</span><br><span class="line">            heroNode = heroNode.getNext();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 构造函数的节点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HeroNode</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> no;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> String nickName;</span><br><span class="line">    <span class="keyword">private</span> HeroNode next;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 初始化构造函数</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> no</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> name</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> nickName</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">HeroNode</span><span class="params">(<span class="keyword">int</span> no, String name, String nickName)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.no = no;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        <span class="keyword">this</span>.nickName = nickName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getNo</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> no;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNo</span><span class="params">(<span class="keyword">int</span> no)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.no = no;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getNickName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> nickName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNickName</span><span class="params">(String nickName)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.nickName = nickName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> HeroNode <span class="title">getNext</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> next;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNext</span><span class="params">(HeroNode next)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.next = next;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"HeroNode&#123;"</span> +</span><br><span class="line">                <span class="string">"no="</span> + no +</span><br><span class="line">                <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", nickName='"</span> + nickName + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", next="</span> + next +</span><br><span class="line">                <span class="string">'&#125;'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-1-2-使用排序的方式插入链表改进上述链表"><a href="#6-1-2-使用排序的方式插入链表改进上述链表" class="headerlink" title="6.1.2 使用排序的方式插入链表改进上述链表"></a>6.1.2 使用排序的方式插入链表改进上述链表</h3><ol>
<li>这里使用了查找的方式，判断是否相等，遍历的时候使用头指针的下一个节点进行遍历操作</li>
<li>插入节点使用被插入的节点的下一个节点指向当前遍历节点的下一个节点，当前循环节点指向指向当前被插入的节点</li>
<li>如果节点相等，给出提示</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 在插入的使用最新的方式：</span></span><br><span class="line"><span class="comment">     * 使用id编号进行排序，在查找的时候使用id进行排序查找</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> node</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addByOrder</span><span class="params">(HeroNode node)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 1. 由于头结点不能动，需要使用</span></span><br><span class="line">        HeroNode heroNode = head;</span><br><span class="line">        <span class="keyword">boolean</span> flag = <span class="keyword">false</span>;</span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span>(heroNode.getNext() == <span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 寻找编号大于被插入节点的节点，因为只能找被插入的节点的上一个节点</span></span><br><span class="line">            <span class="keyword">if</span>(heroNode.getNext().getNo() &gt; node.getNo())&#123;</span><br><span class="line">                <span class="comment">// 获取原来数据的下一个节点一个节点</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span> <span class="keyword">if</span>(heroNode.getNext().getNo() == node.getNo())&#123;</span><br><span class="line">                <span class="comment">//说明希望添加的heroNode的编号已然存在</span></span><br><span class="line">                flag = <span class="keyword">true</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            heroNode = heroNode.getNext();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(flag) &#123;</span><br><span class="line">            System.err.println(<span class="string">"被插入的节点已经存在"</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            node.setNext(heroNode.getNext());</span><br><span class="line">            heroNode.setNext(node);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-1-3-删除节点有多少种情况"><a href="#6-1-3-删除节点有多少种情况" class="headerlink" title="6.1.3 删除节点有多少种情况"></a>6.1.3 删除节点有多少种情况</h3><ol>
<li><p>如果删除的是头节点</p>
</li>
<li><p>如果是中间的节点，需要将被删除的上一个节点执行被删除节点的下一个节点</p>
</li>
<li><p>如果在尾部，删除尾部节点</p>
</li>
</ol>
<h3 id="6-1-4-删除的第一种方式实现办法"><a href="#6-1-4-删除的第一种方式实现办法" class="headerlink" title="6.1.4 删除的第一种方式实现办法"></a>6.1.4 删除的第一种方式实现办法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 删除方式，</span></span><br><span class="line"><span class="comment">     * 第一种方式： 使用编号删除对应的节点</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> no 节点编号</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">del</span><span class="params">(<span class="keyword">int</span> no)</span> </span>&#123;</span><br><span class="line">        HeroNode temp = head;</span><br><span class="line">        <span class="comment">// 如果是头节点指向节点，需要变更头节点指向节点再删除节点</span></span><br><span class="line">        <span class="keyword">if</span> (temp.getNext().getNo() == no) &#123;</span><br><span class="line">            <span class="comment">// 指向下一个节点的下一个节点</span></span><br><span class="line">            head.setNext(head.getNext().getNext());</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 判断是否找到了节点</span></span><br><span class="line">        <span class="keyword">boolean</span> flag = <span class="keyword">false</span>;</span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (temp.getNext().getNo() == no) &#123;</span><br><span class="line">                flag = <span class="keyword">true</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            temp = temp.getNext();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 找到节点进行替换</span></span><br><span class="line">        <span class="keyword">if</span>(flag)&#123;</span><br><span class="line">            temp.setNext(temp.getNext().getNext());</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            System.err.println(<span class="string">"没有找到对应编号的英雄"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-1-5-删除的第二种方式分析："><a href="#6-1-5-删除的第二种方式分析：" class="headerlink" title="6.1.5 删除的第二种方式分析："></a>6.1.5 删除的第二种方式分析：</h3><ol>
<li><p>删除根据第几个来删除，必须我想删除第一个节点</p>
<ol>
<li>第一个节点对应了头节点，需要对于头指针指向进行后移</li>
</ol>
</li>
<li><p>其余根据no编号删除雷同,只需要编号判断改为第几个判断即可</p>
</li>
</ol>
<h2 id="6-2-关于链表的面试题（重点）"><a href="#6-2-关于链表的面试题（重点）" class="headerlink" title="6.2 关于链表的面试题（重点）"></a>6.2 关于链表的面试题（重点）</h2><ol>
<li><p>单链表当中有效节点的个数</p>
</li>
<li><p>获取倒数第n个节点的节点</p>
</li>
<li><p>反转链表</p>
<ol>
<li>定义一个节点：反转用的头</li>
<li>从头到尾遍历原来链表，每遍历一个节点，就取出，放到新的链表前面</li>
<li>将旧链表的下一个节点指向逆序链表的下一个节点</li>
</ol>
</li>
<li><p>从尾部到头部逆序打印链表</p>
<ol>
<li>第一种方法：显逆序，逆序之后打印节点（不可取）</li>
<li>使用栈实现：将所有的节点加入栈当中，然后使用栈进行打印</li>
</ol>
</li>
</ol>
<h3 id="6-2-1-节点个数的实现"><a href="#6-2-1-节点个数的实现" class="headerlink" title="6.2.1 节点个数的实现"></a>6.2.1 节点个数的实现</h3><p>较为简单，遍历统计遍历几次即可</p>
<h3 id="6-2-2-倒数第N个节点"><a href="#6-2-2-倒数第N个节点" class="headerlink" title="6.2.2 倒数第N个节点"></a>6.2.2 倒数第N个节点</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取倒数第index个节点的值</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> index</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> HeroNode <span class="title">getLastIndexHeroNode</span><span class="params">(<span class="keyword">int</span> index)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 1. 获取链表的总个数</span></span><br><span class="line">        <span class="keyword">int</span> count = count();</span><br><span class="line">        <span class="keyword">if</span>(index &lt;= <span class="number">0</span> || index &gt; count)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        HeroNode temp = head.getNext();</span><br><span class="line">        <span class="comment">// 2. 根据size - index , 循环获取倒数第 index 节点</span></span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> size = count-index;</span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == size) &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            i++;</span><br><span class="line">            temp = temp.getNext();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 3. 遍历拿到数据，返回结果</span></span><br><span class="line">        <span class="keyword">return</span> temp;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-2-3-反转链表"><a href="#6-2-3-反转链表" class="headerlink" title="6.2.3 反转链表"></a>6.2.3 反转链表</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 反转节点</span></span><br><span class="line"><span class="comment">     * 1. 定义一个反转节点的head</span></span><br><span class="line"><span class="comment">     * 2. 遍历原来的链表，使用类似入栈的方式，将每个节点接入到反转节点的指向节点，被接入节点指向原来的节点</span></span><br><span class="line"><span class="comment">     * 3. 反转节点完成</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">reverse</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(head == <span class="keyword">null</span> || head.getNext() == <span class="keyword">null</span> || head.getNext().getNext() == <span class="keyword">null</span>)&#123;</span><br><span class="line">            System.err.println(<span class="string">"无需反转"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        HeroNode reverseHead = <span class="keyword">new</span> HeroNode(<span class="number">0</span>, <span class="string">""</span>, <span class="string">""</span>);</span><br><span class="line">        HeroNode temp = head.getNext();</span><br><span class="line">        HeroNode next = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">while</span> (temp != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 让当前节点接到新链表的下一个节点</span></span><br><span class="line">            next = temp.getNext();</span><br><span class="line">            temp.setNext(reverseHead.getNext());</span><br><span class="line">            reverseHead.setNext(temp);</span><br><span class="line">            temp = next;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 使用反转节点替换原来的节点</span></span><br><span class="line">        head.setNext(reverseHead.getNext());</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-2-4-反序打印链表"><a href="#6-2-4-反序打印链表" class="headerlink" title="6.2.4 反序打印链表"></a>6.2.4 反序打印链表</h3><ol>
<li>需要使用到栈这种结构，将节点压进栈当中，然后从栈中取数据进行遍历</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 反序打印链表里面的内容</span></span><br><span class="line"><span class="comment">     * 使用的栈结构进行存放数据，然后使用栈进行输出操作</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">reversePrint</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.err.println();</span><br><span class="line">        <span class="keyword">if</span> (head == <span class="keyword">null</span> || head.getNext() == <span class="keyword">null</span> || head.getNext().getNext() == <span class="keyword">null</span>) &#123;</span><br><span class="line">            System.err.println(<span class="string">"无需反转打印"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        HeroNode hero = head.getNext();</span><br><span class="line">        Stack&lt;HeroNode&gt; stack = <span class="keyword">new</span> Stack&lt;&gt;();</span><br><span class="line">        <span class="keyword">while</span> (hero != <span class="keyword">null</span>) &#123;</span><br><span class="line">            stack.push(hero);</span><br><span class="line">            hero = hero.getNext();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span>(stack.size()&gt;<span class="number">0</span>) &#123;</span><br><span class="line">            System.out.println(stack.pop());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-2-5-合并两个链表"><a href="#6-2-5-合并两个链表" class="headerlink" title="6.2.5 合并两个链表"></a>6.2.5 合并两个链表</h3><ol>
<li>遍历两个链表，先将第一个链表进行插入，然后根据第二个链表进行相同的插入操作s</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 合并两个链表，合并之后依然有序</span></span><br><span class="line"><span class="comment">     * 1. 计算两个链表的个数</span></span><br><span class="line"><span class="comment">     * 2. 循环总数，判断两个节点的当前节点更大，</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> list1</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> list2</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> SingelLinkedList <span class="title">mergeList</span><span class="params">(SingelLinkedList list1, SingelLinkedList list2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(list1 == <span class="keyword">null</span> &amp;&amp; list2 == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(list1 == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> list2;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(list2 == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> list1;</span><br><span class="line">        &#125;</span><br><span class="line">        SingelLinkedList result = <span class="keyword">new</span> SingelLinkedList();</span><br><span class="line">        HeroNode next1 = list1.getHead();</span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (next1 == <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            HeroNode next = next1.getNext();</span><br><span class="line">            SingelLinkedList.addByOrder(result, next1);</span><br><span class="line">            next1 = next;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        HeroNode next2 = list2.getHead().getNext();</span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(next2 == <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            HeroNode next = next2.getNext();</span><br><span class="line">            SingelLinkedList.addByOrder(result, next2);</span><br><span class="line">            next2 = next;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="6-3-分析双向链表的遍历，添加、删除的操作思路"><a href="#6-3-分析双向链表的遍历，添加、删除的操作思路" class="headerlink" title="6.3 分析双向链表的遍历，添加、删除的操作思路"></a>6.3 分析双向链表的遍历，添加、删除的操作思路</h2><ol>
<li>遍历方和单链表一样只是可以朝前，可以向后</li>
<li>添加<ol>
<li>默认添加到双向链表的最后</li>
<li>先找到双向链表的最后这个节点</li>
<li>temp.next = new heronode</li>
<li>newheronode.pre = temp</li>
</ol>
</li>
<li>删除<ol>
<li>因为是双向的，可以自我删除</li>
<li>找到要删除的这个节点，temp</li>
<li>temp.next.pre = temp.pre</li>
<li>temp.pre.next = temp.next</li>
</ol>
</li>
<li>修改<ol>
<li>不需要太大变动，和原来的链表类似</li>
</ol>
</li>
</ol>
<h2 id="6-4-循环链表（约瑟夫问题）"><a href="#6-4-循环链表（约瑟夫问题）" class="headerlink" title="6.4 循环链表（约瑟夫问题）"></a>6.4 循环链表（约瑟夫问题）</h2><h3 id="6-4-1-什么是循环链表"><a href="#6-4-1-什么是循环链表" class="headerlink" title="6.4.1 什么是循环链表"></a>6.4.1 什么是循环链表</h3><p>无论是加入还是删除节点，最后一个节点要么指向自己，要么指向链表的头节点，形成环状的一个链表</p>
<h3 id="6-4-2-构建一个环形链表的思路"><a href="#6-4-2-构建一个环形链表的思路" class="headerlink" title="6.4.2 构建一个环形链表的思路"></a>6.4.2 构建一个环形链表的思路</h3><ol>
<li>先创建一个节点，first 指向改节点， 并且形成环形</li>
<li>后面当我们每创建一个新的节点，就把该节点，加入到已有的环形链表当中即可</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 环形链表的添加和原来的添加代码有所不同</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> size</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> size)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(size &lt; <span class="number">1</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">"size的值不正确"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        CircularNode cur = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= size; i++) &#123;</span><br><span class="line">            CircularNode newcur = <span class="keyword">new</span> CircularNode(i, <span class="string">"小孩"</span>+i, <span class="string">"小孩昵称"</span>+i);</span><br><span class="line">            <span class="comment">// 如果是第一个节点，则需要特殊处理</span></span><br><span class="line">            <span class="keyword">if</span>(i == <span class="number">1</span>)&#123;</span><br><span class="line">                <span class="comment">// 第一个节点复制给first节点</span></span><br><span class="line">                first = newcur;</span><br><span class="line">                first.setNext(first);</span><br><span class="line">                <span class="comment">// 将当前操作指针指向第一个节点</span></span><br><span class="line">                cur = first;</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                cur.setNext(newcur);</span><br><span class="line">                newcur.setNext(first);</span><br><span class="line">                cur = newcur;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>



<h3 id="6-4-3-遍历环形链表"><a href="#6-4-3-遍历环形链表" class="headerlink" title="6.4.3 遍历环形链表"></a>6.4.3 遍历环形链表</h3><ol>
<li>先让一个辅助指针 curBoy ， 指向first</li>
<li>通过while循环，遍历环形链表，当curBoy.next  == first 结束</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 遍历节点</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">list</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(first == <span class="keyword">null</span>)&#123;</span><br><span class="line">            System.err.println(<span class="string">"循环链表为空"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        CircularNode circularQueue = first;</span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;</span><br><span class="line">            System.err.println(circularQueue);</span><br><span class="line">            <span class="keyword">if</span>(circularQueue.getNext() == first)&#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            circularQueue = circularQueue.getNext();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>



<h3 id="6-4-4-模拟约瑟夫问题的实现（重点）"><a href="#6-4-4-模拟约瑟夫问题的实现（重点）" class="headerlink" title="6.4.4 模拟约瑟夫问题的实现（重点）"></a>6.4.4 模拟约瑟夫问题的实现（重点）</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> begin 从第几个小孩开始数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> countNum 数几个小孩</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> nums 表示最初有多少小孩在圈中</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">count</span><span class="params">(<span class="keyword">int</span> begin, <span class="keyword">int</span> countNum, <span class="keyword">int</span> nums)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 先对数据进行校验</span></span><br><span class="line">        <span class="keyword">if</span> (first == <span class="keyword">null</span> || begin &lt; <span class="number">1</span> || countNum &gt; nums) &#123;</span><br><span class="line">            System.out.println(<span class="string">"参数输入有误， 请重新输入"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 创建辅助指针，指向环形链表的最后一个元素</span></span><br><span class="line">        CircularNode helper = first;</span><br><span class="line">        <span class="keyword">while</span>(helper.getNext() != first)&#123;</span><br><span class="line">            helper = helper.getNext();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//小孩报数前，先让 first 和  helper 移动 k - 1次</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; begin; i++) &#123;</span><br><span class="line">            first = first.getNext();</span><br><span class="line">            helper = helper.getNext();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 当小孩报数时，让first 和 helper 指针同时 的移动  m  - 1 次, 然后出圈</span></span><br><span class="line">        <span class="comment">// 这里是一个循环操作，知道圈中只有一个节点</span></span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(helper == first) &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//让 first 和 helper 指针同时 的移动 countNum - 1</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; countNum - <span class="number">1</span>; i++) &#123;</span><br><span class="line">                first = first.getNext();</span><br><span class="line">                helper = helper.getNext();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 这时first指向的节点，就是要出圈的小孩节点\</span></span><br><span class="line">            System.out.printf(<span class="string">"小孩%d出圈\n"</span>, first.getNo());</span><br><span class="line">            first = first.getNext();</span><br><span class="line">            helper.setNext(first);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.printf(<span class="string">"最后留在圈中的小孩编号%d \n"</span>, first.getNo());</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h1 id="7-栈"><a href="#7-栈" class="headerlink" title="7. 栈"></a>7. 栈</h1><h2 id="7-1-如何实现一个栈结构"><a href="#7-1-如何实现一个栈结构" class="headerlink" title="7.1 如何实现一个栈结构"></a>7.1 如何实现一个栈结构</h2><ol>
<li><p>使用一个top 初始化为 -1 表示栈定， 压入数据 为 push + 1</p>
</li>
<li><p>使用 top – 表示出栈</p>
</li>
<li><p>由于栈帧只能从一个方向操作，则需要对于栈帧进行一下判断是否为空或者已满</p>
</li>
</ol>
<h2 id="7-2-使用栈模拟一个表达式计算（重点）"><a href="#7-2-使用栈模拟一个表达式计算（重点）" class="headerlink" title="7.2 使用栈模拟一个表达式计算（重点）"></a>7.2 使用栈模拟一个表达式计算（重点）</h2><ol>
<li>首先：使用一个索引扫描整个表达式，用于往两个栈中添加数据</li>
<li>如果是数字，则直接加入到数栈当中</li>
<li>如果是操作符，则分为以下两种情况<ol>
<li>如果是加法和减法，则直接加入到栈顶</li>
<li>如果是乘法或者除法，则压如下一个表达式索引值</li>
</ol>
</li>
</ol>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>lazytime
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://whitestore.top/2020/10/27/study/" title="数据结构和算法的一个留坑">https://whitestore.top/2020/10/27/study/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLzQuMC96aC1DTg=="><i class="fa fa-fw fa-creative-commons"></i>BY-NC</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" rel="tag"># 数据结构与算法</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/10/27/goidea/" rel="prev" title="使用Go语言+IDEA+mysql 逆向生成dao 和 domain 的方法">
      <i class="fa fa-chevron-left"></i> 使用Go语言+IDEA+mysql 逆向生成dao 和 domain 的方法
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/10/27/code/" rel="next" title="雪花算法代码">
      雪花算法代码 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-数据结构与算法入门"><span class="nav-number">1.</span> <span class="nav-text">1. 数据结构与算法入门</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-经典算法问题："><span class="nav-number">1.2.</span> <span class="nav-text">1.1 经典算法问题：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-字符串匹配"><span class="nav-number">1.3.</span> <span class="nav-text">1.2 字符串匹配</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-1-暴力匹配"><span class="nav-number">1.3.1.</span> <span class="nav-text">1.2.1 暴力匹配</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-2-KMP算法"><span class="nav-number">1.3.2.</span> <span class="nav-text">1.2.2 KMP算法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-数据结构和算法重要性"><span class="nav-number">1.4.</span> <span class="nav-text">1.3 数据结构和算法重要性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-数据结构与算法关系"><span class="nav-number">1.5.</span> <span class="nav-text">1.4 数据结构与算法关系</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-实际算法问题："><span class="nav-number">2.</span> <span class="nav-text">2. 实际算法问题：</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-str-replaceAll-str"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 str.replaceAll( str )</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-1-问题："><span class="nav-number">2.1.1.</span> <span class="nav-text">2.1.1 问题：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-其他几个问题："><span class="nav-number">2.2.</span> <span class="nav-text">2.2 其他几个问题：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-线性结构与非线性结构"><span class="nav-number">2.3.</span> <span class="nav-text">2.3 线性结构与非线性结构</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-1-非线性结构"><span class="nav-number">2.3.1.</span> <span class="nav-text">2.3.1 非线性结构</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-稀疏数组和队列"><span class="nav-number">3.</span> <span class="nav-text">3. 稀疏数组和队列</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-稀疏数组的处理方法："><span class="nav-number">3.1.</span> <span class="nav-text">3.1 稀疏数组的处理方法：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-1-二维数组转稀疏数组的方法"><span class="nav-number">3.1.1.</span> <span class="nav-text">3.1.1 二维数组转稀疏数组的方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-2-稀疏数组转二维数组："><span class="nav-number">3.2.</span> <span class="nav-text">3.2 稀疏数组转二维数组：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-稀疏数组的Java代码实现"><span class="nav-number">4.</span> <span class="nav-text">4. 稀疏数组的Java代码实现</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-队列："><span class="nav-number">5.</span> <span class="nav-text">5. 队列：</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#5-1-数组模拟队列"><span class="nav-number">5.1.</span> <span class="nav-text">5.1 数组模拟队列</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-1-思路分析：addQueue"><span class="nav-number">5.1.1.</span> <span class="nav-text">5.1.1 思路分析：addQueue</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-2-思路分析：removeQueue"><span class="nav-number">5.1.2.</span> <span class="nav-text">5.1.2 思路分析：removeQueue</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-2-JAVA代码实现数组底层的队列"><span class="nav-number">5.2.</span> <span class="nav-text">5.2 JAVA代码实现数组底层的队列</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-1-数组队列的弊端"><span class="nav-number">5.2.1.</span> <span class="nav-text">5.2.1 数组队列的弊端</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-3-数组实现循环队列"><span class="nav-number">5.3.</span> <span class="nav-text">5.3  数组实现循环队列</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-1-如何判定队列已经满了"><span class="nav-number">5.3.1.</span> <span class="nav-text">5.3.1 如何判定队列已经满了</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-2-如何判定队列是空的"><span class="nav-number">5.3.2.</span> <span class="nav-text">5.3.2 如何判定队列是空的</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-3-如何判定有效个数"><span class="nav-number">5.3.3.</span> <span class="nav-text">5.3.3 如何判定有效个数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-3-4-使用数组实现循环队列的方式2"><span class="nav-number">5.4.</span> <span class="nav-text">5.3.4 使用数组实现循环队列的方式2</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6-链表"><span class="nav-number">6.</span> <span class="nav-text">6. 链表</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#6-1-使用自制的链表处理"><span class="nav-number">6.1.</span> <span class="nav-text">6.1 使用自制的链表处理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-1-使用java代码实现链表"><span class="nav-number">6.1.1.</span> <span class="nav-text">6.1.1 使用java代码实现链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-2-使用排序的方式插入链表改进上述链表"><span class="nav-number">6.1.2.</span> <span class="nav-text">6.1.2 使用排序的方式插入链表改进上述链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-3-删除节点有多少种情况"><span class="nav-number">6.1.3.</span> <span class="nav-text">6.1.3 删除节点有多少种情况</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-4-删除的第一种方式实现办法"><span class="nav-number">6.1.4.</span> <span class="nav-text">6.1.4 删除的第一种方式实现办法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-5-删除的第二种方式分析："><span class="nav-number">6.1.5.</span> <span class="nav-text">6.1.5 删除的第二种方式分析：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-2-关于链表的面试题（重点）"><span class="nav-number">6.2.</span> <span class="nav-text">6.2 关于链表的面试题（重点）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-1-节点个数的实现"><span class="nav-number">6.2.1.</span> <span class="nav-text">6.2.1 节点个数的实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-2-倒数第N个节点"><span class="nav-number">6.2.2.</span> <span class="nav-text">6.2.2 倒数第N个节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-3-反转链表"><span class="nav-number">6.2.3.</span> <span class="nav-text">6.2.3 反转链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-4-反序打印链表"><span class="nav-number">6.2.4.</span> <span class="nav-text">6.2.4 反序打印链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-5-合并两个链表"><span class="nav-number">6.2.5.</span> <span class="nav-text">6.2.5 合并两个链表</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-3-分析双向链表的遍历，添加、删除的操作思路"><span class="nav-number">6.3.</span> <span class="nav-text">6.3 分析双向链表的遍历，添加、删除的操作思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-4-循环链表（约瑟夫问题）"><span class="nav-number">6.4.</span> <span class="nav-text">6.4 循环链表（约瑟夫问题）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-4-1-什么是循环链表"><span class="nav-number">6.4.1.</span> <span class="nav-text">6.4.1 什么是循环链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-4-2-构建一个环形链表的思路"><span class="nav-number">6.4.2.</span> <span class="nav-text">6.4.2 构建一个环形链表的思路</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-4-3-遍历环形链表"><span class="nav-number">6.4.3.</span> <span class="nav-text">6.4.3 遍历环形链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-4-4-模拟约瑟夫问题的实现（重点）"><span class="nav-number">6.4.4.</span> <span class="nav-text">6.4.4 模拟约瑟夫问题的实现（重点）</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#7-栈"><span class="nav-number">7.</span> <span class="nav-text">7. 栈</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#7-1-如何实现一个栈结构"><span class="nav-number">7.1.</span> <span class="nav-text">7.1 如何实现一个栈结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-2-使用栈模拟一个表达式计算（重点）"><span class="nav-number">7.2.</span> <span class="nav-text">7.2 使用栈模拟一个表达式计算（重点）</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">阿东</p>
  <div class="site-description" itemprop="description">随遇而安</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">239</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lazyTimes"><i class="fa fa-fw fa-github"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOjEwOTc0ODM1MDhAcXEuY29t" title="E-Mail → mailto:1097483508@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly93d3cuNTJwb2ppZS5jbi9ob21lLnBocD9tb2Q9c3BhY2UmdWlkPTE0OTc3MTgmZG89dGhyZWFkJnZpZXc9bWUmZnJvbT1zcGFjZQ==" title="https:&#x2F;&#x2F;www.52pojie.cn&#x2F;home.php?mod&#x3D;space&amp;uid&#x3D;1497718&amp;do&#x3D;thread&amp;view&#x3D;me&amp;from&#x3D;space">吾爱破解</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9qdWVqaW4uaW0vdXNlci8yOTk5MTIzNDUyNjI2MzY2" title="https:&#x2F;&#x2F;juejin.im&#x2F;user&#x2F;2999123452626366">掘金</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9zZWdtZW50ZmF1bHQuY29tL3UvbGF6eXRpbWVz" title="https:&#x2F;&#x2F;segmentfault.com&#x2F;u&#x2F;lazytimes">思否</span>
        </li>
    </ul>
  </div>

      </div>

      <div class="wechat_OA">
        <span>欢迎关注我的公众号</span>
        <br>
          <!-- 这里添加你的二维码图片 -->
        <img src ="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/wechat_channel.jpg">
      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">阿东</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">2m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">29:50</span>
</div>
  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Gemini</span> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'qMUpEEvBgXaMDD1b0ftgi9xr-gzGzoHsz',
      appKey     : 'UCdfT4Rfih6MO6y8DI4fstf6',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
